package solitaire.deck;

import java.util.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import solitaire.extras.*;
import javax.imageio.ImageIO;

import org.omg.CORBA.PRIVATE_MEMBER;


public class Card extends DrawObject {

	public enum Rank {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING}
	public enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES}
	
	public static String DC_STRING = "pic//b1fv.gif";
	private String turnedString;
	// Rank of the card from ace to king
	private final Rank rank;
	
	// To which suit the card belongs*/
	private Suit suit;
	
	// The image the card displays */
	private BufferedImage cardImage;
	
	private double storeX;
	private double storeY;
	private boolean isTurned;
	
	
	// Constructor for the card
	public Card(Rank rank, Suit suit)
	{
		isTurned = false;
		this.rank = rank;
		this.suit = suit;
		
		try
        {
            cardImage = ImageIO.read( new File(DC_STRING) );
        }
        catch(IOException ex)
        {
            System.out.println("Could not find picture:" + DC_STRING);
            System.exit( -1 );
        }
	}
	
	// Get the rank of the card
	public Rank rank()
	{
		return rank;
	}
	
	// Gets the suit of the card
	public Suit suit()
	{
		return suit;
	}
	
	// Draws the card on the screen 
	public void drawObject(Graphics g)
	{
		Graphics2D g2 = ( Graphics2D)g;
        g2.drawRenderedImage( cardImage, AffineTransform.getTranslateInstance( 
        		m_dStartX - cardImage.getWidth()/2 , 
        		m_dStartY - cardImage.getHeight()/2 ) );
	}
	
	// Returns true if the given point where the card is positioned 
	public boolean contains(Point2D point)
	{
        return 
        	point.getX() >= (m_dStartX - cardImage.getWidth()/2) && 
        	point.getX() <= (m_dStartX + cardImage.getWidth()/2) &&
        	point.getY() >= (m_dStartY - cardImage.getHeight()/2) && 
        	point.getY() <= (m_dStartY + cardImage.getHeight()/2);
	}
	
	// Set a new location for the card 
	public void setLocation(double dX, double dY)
	{
		m_dStartX = dX;
        m_dStartY = dY;
	}			
	
	// Changes the image of the card to fit the suit and rank for this card.
	public void turnCard()
	{ 
		isTurned = true;
		int suit = (Card.this.suit.ordinal()+1);
		int rank = (Card.this.rank.ordinal()+1);
		turnedString = "pic//" + suit + "-" + rank + ".gif";
		
         try 
         {
        	 cardImage = ImageIO.read( new File(turnedString) );
         }
         catch(IOException ex)
         {
             System.out.println("Could not find picture:" + turnedString);
             System.exit( -1 );
         }

	}
	
	public boolean getIsTurned()
	{
		return isTurned;
	}
	
	public void storeLocation()
	{
		storeX = m_dStartX;
		storeY = m_dStartY;
	}
	
	public void restoreLocation()
	{
		m_dStartX = storeX;
		m_dStartY = storeY;
	}
	
	// Override method, displays rank and suit of card
	public String toString()
	{
		return rank + " of " + suit + " x = " + m_dStartX + " y = " + m_dStartY;
		
	}
}
